// packageC/redPacket/personRedList/personRedList.js
const app = getApp();
Page({

  data: {
    tabActive: 1,
    requestRul: 'plugin.redpack-user.frontend.sendLog.index',

    total: 0,
    page: 1,
    finished: false,
  },

  onLoad: function (options) {

  },

  /**
     * 生命周期函数--监听页面显示
     */
  onShow: function () {
    this.getData();
  },

  getData() {
    let urlStr = app.getNetAddresss(this.data.requestRul);
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
          });
          return;
        }
        let data = res.data.data.list;
        let finished = false;
        if (this.data.page >= data.last_page || data.data.length < data.per_page) {
          finished = true;
        }
        this.setData({
          page: (++this.data.page),
          finished,
          recordList: data.data
        });
      }
    });
  },

  getMoreData () {
    if (this.data.finished) return;
    let urlStr = app.getNetAddresss(this.data.requestRul);
    wx.showLoading({
      title: '加载中',
    });
    app._getNetWork({
      url: urlStr,
      data: {
        page: this.data.page
      },
      success: (res) => {
        wx.hideLoading();
        if (res.data.result !== 1) {
          wx.showToast({
            title: res.data.msg,
          });
          return;
        }
        let data = res.data.data.list;
        let finished = false;
        let recordList = this.data.recordList.concat(data.data);
        if (this.data.page >= data.last_page || data.data.length < data.per_page) {
          finished = true;
        }
        this.setData({
          page: (++this.data.page),
          finished,
          recordList
        });
      },
      fail () {
        wx.hideLoading();
      }
    });
  },

  onChange (event) {
    let requestRul = '';
    if (event.detail.name == 1) {
      requestRul = 'plugin.redpack-user.frontend.sendLog.index';
    } else {
      requestRul = 'plugin.redpack-user.frontend.receiveLog.index';
    }
    this.setData({
      page: 1,
      finished: false,
      requestRul,
      tabActive: event.detail.name,
    });
    this.getData();
  },
  goPersonRedDetail (event) {
    let rid = event.currentTarget.dataset.rid;
    wx.navigateTo({
      url: `/packageI/personRed/personListDetail/personListDetail?rid=${rid}`,
    });
  },
  goPersonRedRecord (event) {
    let rid = event.currentTarget.dataset.rid;
    wx.navigateTo({
      url: `/packageI/personRed/personRedRecord/personRedRecord?rid=${rid}`,
    });
  },
    
  // 触底加载更多
  onReachBottom: function () {
    this.getMoreData();
  },
});